package com.runda.system.controller;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.bbjob.dto.Result;
import com.bbjob.model.SysFunction;
import com.runda.entity.JwtUser;
import com.runda.sec.SecContextUtil;
import com.runda.system.service.MenuService;
import com.runda.util.DataEntity;


@RestController
@RequestMapping("sys/menu")
public class MenuController {
	
	@Resource
	private MenuService menuservice;
	
	@RequestMapping("/aaa")
	@ResponseBody
	public String home() {
		StringBuffer sb= new StringBuffer();
		sb.append("<form id='form1' action='http://127.0.0.1:8888/upload' method='post' enctype='multipart/form-data'>");   
		sb.append("文件名<input id='name' name='name' type='text' value='root.pdf'/></br>");   
		//sb.append("总大小<input id='size' name='size' type='text'value='10000'/></br>");   
		sb.append("总片数<input id='total' name='total' type='text'value='1'/></br>");   
		sb.append("当前片数<input id='index' name='index' type='text'value='1'/></br>");
		sb.append("资源等级<input id='index' name='resLevel' type='text'value='1'/></br>");   
		sb.append("总MD5<input id='fileMd5' name='fileMd5' type='text'value='ffec78f47b76096e64c381412ae82d1'/></br>");   
		sb.append("分片MD5<input id='md5' name='md5' type='text'value='ffec78f47b76096e64c381412ae82d1'/></br>");   
		sb.append("上传的文件<input id='file' name='file' type='file' /></br>");   
		sb.append("<input type='submit' value='提交' />");   
		return sb.toString();
	}
	
	
	@RequestMapping(value = "saveMenu", method = RequestMethod.POST)
	@ResponseBody
	public Result saveMenu(@RequestBody SysFunction menu) {
		int ok = menuservice.saveMenu(menu);
		if(ok > 0) {
			return new Result(200, "保存成功");
		}else {
			return new Result(8106,"保存失败");
		}
	}
	
	@RequestMapping(value = "editMenu", method = RequestMethod.POST)
	@ResponseBody
	public Result editMenu(@RequestBody SysFunction menu) {
		int ok = menuservice.editMenu(menu);
		if(ok > 0) {
			return new Result(200, "修改成功");
		}else {
			return new Result(8106,"修改失败");
		}
	}
	
	@RequestMapping(value = "deleteMenu", method = RequestMethod.GET)
	@ResponseBody
	public Result deleteUser(Integer id) {
		int ok = menuservice.deleteMenu(id);
		if(ok > 0) {
			return new Result(200, "删除成功");
		}else {
			return new Result(8106,"删除失败");
		}
	}
	
	@RequestMapping(value = "getTree", method = RequestMethod.GET)
	@ResponseBody
	public DataEntity getTree() {
		return DataEntity.getInstance(menuservice.selectTreeByPid(null));
	}
	
	@RequestMapping(value = "getMotherMenu", method = RequestMethod.GET)
	@ResponseBody
	public DataEntity getMotherMenu(Integer userid) {
		return DataEntity.getInstance(menuservice.getMotherMenu(userid));
	}
	
	@RequestMapping(value = "getOperationAuth", method = RequestMethod.GET)
	@ResponseBody
	public DataEntity getOperationAuth() {
		JwtUser user = SecContextUtil.getCurrentUser();
		return DataEntity.getInstance(menuservice.getOperationAuth(user.getId()));
	}
	
	
}
